/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.senai.config;

import br.com.senai.view.OpcoesBanco;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import org.hibernate.HibernateException;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;

/**
 * Hibernate Utility class with a convenient method to get Session Factory
 * object.
 *
 * @author Hugo
 */
public class HibernateUtil {

    private static final SessionFactory sessionFactory;

    static {
        try {
            // Create the SessionFactory from standard (hibernate.cfg.xml) 
            // config file.
            Config.carregarConexao();
       
                sessionFactory = new AnnotationConfiguration().configure().setProperty("hibernate.connection.password", Config.senha).setProperty("hibernate.connection.username", Config.usuario).setProperty("hibernate.connection.url", "jdbc:mysql://" + Config.url + "/sistemadepaes?zeroDateTimeBehavior=convertToNull").
                        buildSessionFactory();
     
        } catch (HibernateException ex) {
            // Log the exception. 
            System.err.println("Initial SessionFactory creation failed." + ex);
            JOptionPane.showMessageDialog(null, "FALHA AO INICIAR O BANCO", "erro", JOptionPane.ERROR_MESSAGE);
            new OpcoesBanco().setVisible(true);
         
            throw new ExceptionInInitializerError(ex);

        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}
